﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Tsina
{
    public partial class IMEButtonControl : UserControl
    {
        public IMEButtonControl()
        {
            InitializeComponent();
            IsEnable = true;
            this.border.MouseLeftButtonDown += border_MouseLeftButtonDown;
            this.border.MouseLeftButtonUp += border_MouseLeftButtonUp;
        }

        private void border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (isEnable == true)
            {
                Border border = sender as Border;
                border.Background = whiteBrush;
                (border.Child as TextBlock).Foreground = blackBrush;
                border.CaptureMouse();
            }
        }

        private void border_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (isEnable == true)
            {
                Border border = sender as Border;
                if (border.CaptureMouse() == true)
                {
                    border.Background = null;
                    (border.Child as TextBlock).Foreground = whiteBrush;
                    border.ReleaseMouseCapture();
                    Point mousePosition = e.GetPosition(border);
                    if (mousePosition.X >= 0 && mousePosition.X <= border.ActualWidth && mousePosition.Y >= 0 && mousePosition.Y <= border.ActualHeight)
                    {
                        if (Click != null)
                        {
                            Click(this, null);
                        }
                    }
                }
            }
        }

        private static SolidColorBrush whiteBrush = new SolidColorBrush(Color.FromArgb(0xff, 0xff, 0xff, 0xff));
        private static SolidColorBrush blackBrush = new SolidColorBrush(Color.FromArgb(0xff, 0x00, 0x00, 0x00));
        private static SolidColorBrush grayBrush = new SolidColorBrush(Color.FromArgb(0xff, 0x79, 0x79, 0x79));

        public string Text
        {
            get 
            {
                return this.textBlock.Text;
            }
            set 
            {
                this.textBlock.Text = value;
            }
        }

        public TextAlignment TextAlignment
        {
            get
            {
                return this.textBlock.TextAlignment;
            }
            set
            {
                this.textBlock.TextAlignment = value;
            }
        }

        public Thickness TextPadding
        {
            get
            {
                return this.textBlock.Padding;
            }
            set
            {
                this.textBlock.Padding = value;
            }
        }

        private bool isEnable;
        public bool IsEnable
        {
            get
            {
                return isEnable;
            }
            set
            {
                if (isEnable != value)
                {
                    isEnable = value;
                    if (value == true)
                    {
                        this.textBlock.Foreground = whiteBrush;
                    }
                    else
                    {
                        this.textBlock.Foreground = grayBrush;
                    }
                }
            }
        }

        public event EventHandler Click;
    }
}
